package com.example.administrator.yunniuusedcar.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.administrator.common.base.Global;
import com.example.administrator.yunniuusedcar.adapter.MainAdapter;
import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.adapter.MainInfoAdapter;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.WZ_ArticleInfoBean;
import com.example.administrator.yunniuusedcar.model.mode_benn.WZ_ArticlenListBean;
import com.example.administrator.yunniuusedcar.model.mode_benn.ZD_ArticleTopBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Unbinder;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/11/28.
 * 2 咨询 （viewpager）
 */

public class MainInformationFragment2Model extends BaseFragment {

    private View view;
    private Unbinder unbinder;
    private TabLayout tb;
    private ViewPager vp;
    private ZD_ArticleTopBean zdArticleTopBean;
    private ArrayList listDatas = new ArrayList();
    private RecyclerView recyclerView;
    private MainInfoAdapter mainInfoAdapter;
    public MainInformationFragment2Model mainInformationFragment2Model;

    @Override
    public int getLayoutRes() {    //资讯
        return R.layout.fragment_informationcar2;   //  fragment_choosecar02_information  fragment_choosecar02_news
    }

    @Override
    public void initView() {
        initTablayout();
        initViewPage();
        initFindByID();

        mainInformationFragment2Model = this;

        // 请求网络：
   //     initUloadInfoList();   //文章置顶
    //    initUloadInfoLisr();   // 文章列表
//        initUloadMenuInfor();    //// 文章详情


    }

    private void initFindByID() {   //找控件
        recyclerView = findView(R.id.recycler_view66);
        mainInfoAdapter = new MainInfoAdapter(getContext(),listDatas);
        recyclerView.setAdapter(mainInfoAdapter);


    }

    private String[] titles = {"当日头条", "活动咨询"};

    private void initViewPage() {
        vp = findView(R.id.vp_infornCar);
        List<Fragment> fs = new ArrayList<>();
            fs.add(new FragmentNews02());
            fs.add(new FragmentInfor02());

        MainAdapter mainAdapter1 = new MainAdapter(getChildFragmentManager(), fs, titles);
        vp.setAdapter(mainAdapter1);
        tb.setupWithViewPager(vp);

    }

    private void initTablayout() {
        tb = findView(R.id.tablayout_infocar);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    private void initUloadInfoList() {
        //  网址：  http://carshop.cloudabull.com/api/Index/getrandList?sign=324c41l+5prUictRCX6S5Y2gjmYZ5W5zK6nrvVSTdbbrVpaYVrAnqVROgVSHUW8
        String ull = IHttpService.HOST_URL + "Article/getTop";    // 文章置顶

        OkHttpUtils.get().url(ull).addParams("sign",IHttpService.SignVal).build().execute(new StringCallback() {


            @Override
            public void onError(Call call, Exception e, int id) {
                System.out.println("-----====----------B"+ e);
            }

            @Override
            public void onResponse(String response, int id) {
                System.out.println("-----====-----------B1"+response);
                Gson jd = new Gson();
                zdArticleTopBean = jd.fromJson(response, ZD_ArticleTopBean.class);
              //  Global.showToast("======----=--==" + response);

                listDatas.add(zdArticleTopBean);
                mainInfoAdapter.setDatas(listDatas);
            }
        });
    }
//    private void initUloadMenuInfor() {
//
//        String ul = IHttpService.HOST_URL + "Article/getInfo";    // 文章详情
//        String vals = "06a96b0bKp0f6fx4kdONF5h3ywpqULc+FZTxA44FJ10ZHdCsQs+u7iFizfbnOjU";
//
//        OkHttpUtils.get().url(ul).addParams("sign",vals).addParams("article_id","2").build().execute(new StringCallback() {     //.addParams("type","1")
//
//            @Override
//            public void onError(Call call, Exception e, int id) {
//      //          Global.showToast("请求超时！"+ e.getMessage());
//                //      System.out.println("-----====----------A"+ e);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//      ////        Global.showToast("====="+response);
//                //      System.out.println("-----====-----------A1"+response);
//                Gson js = new Gson();
//                WZ_ArticleInfoBean wzArticleInfoBean = js.fromJson(response,WZ_ArticleInfoBean.class);
//                listDatas.add(wzArticleInfoBean);
//                mainInfoAdapter.setDatas(listDatas);
//
//
//            }
//        });
//    }
    private void initUloadInfoLisr() {

        String ule = IHttpService.HOST_URL + "Article/getList";    // 文章列表 y

       //  cf3cRaXM77oBKdsAGGL7Dxo+9OxWCPRskuNQHEysIdz8hXJvBbUJEpS+oK+2BNA

        OkHttpUtils.get().url(ule).addHeader("class_id","1").addParams("sign",IHttpService.SignVal).addParams("class_id","2").build().execute(new StringCallback() {
            int code ;   //1 成功   0 失败
            //            if(code == 1){    //
//                return
//            }else{
//
//            };
            @Override
            public void onError(Call call, Exception e, int id) {
      //          Global.showToast("请求超时..！"+ e.getMessage());

                System.out.println("-----====-----------D"+e);
            }

            @Override
            public void onResponse(String response, int id) {

                System.out.println("-----====-----------D1"+response);
          ////      Global.showToast("====="+response);
                Gson js = new Gson();
                WZ_ArticlenListBean wzArticlenListBean = js.fromJson(response,WZ_ArticlenListBean.class);
                listDatas.add(wzArticlenListBean);
                mainInfoAdapter.setDatas(listDatas);
            }

        });
    }
//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        view = View.inflate(getActivity(), R.layout.fragment_choosecar02_news, null);
//    }
//
//    @Nullable
//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        // TODO:OnCreateView Method has been created, run ButterKnife again to generate code
//        unbinder = ButterKnife.bind(this, view);
//        return view;
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        unbinder.unbind();
//    }
}
